package com.sali.rule;


public class TimestampRule implements Rule {
    private static final long TIMESTAMP_10_LENGTH_MAX = 9999999999L;
    private static final int TIMESTAMP_10 = 10;
    private static final int TIMESTAMP_13 = 13;
    private static Rule rule;

    static {
        rule = new TimestampRule();
    }

    @Override
    public Object invoke(Object value) {
        Long timeStamp;
        if (value instanceof Long) {
            timeStamp = (Long) value;
            if (timeStamp < TIMESTAMP_10_LENGTH_MAX) {
                timeStamp = timeStamp * 1000;
                return timeStamp;
            }
        } else if (value instanceof String) {
            String tempValue = (String) value;
            if (tempValue.length() == TIMESTAMP_10) {
                timeStamp = Long.valueOf(tempValue + "000");
                return timeStamp;
            } else if (tempValue.length() == TIMESTAMP_13) {
                timeStamp = Long.valueOf(tempValue);
                return timeStamp;
            }
        }
        return null;
    }

    /**
     * 获取实例方法
     *
     * @return
     */
    public static Rule getInstance() {
        return rule;
    }
}
